﻿//Register scripts

var regName = "";
var userExists = false;

function setCurrentItem(row_id) {
    document.getElementById("row_user").style.backgroundColor = "";
    document.getElementById("row_nickname").style.backgroundColor = "";
    document.getElementById("row_pass").style.backgroundColor = "";
    document.getElementById("row_pass_confirm").style.backgroundColor = "";
    document.getElementById("row_mail").style.backgroundColor = "";
    document.getElementById("row_yzm").style.backgroundColor = "";
    document.getElementById(row_id).style.backgroundColor = "#EDF2F7";
}

//check username
function checkUserNameInternal(user_account) {
    var user_name = user_account.value.toLowerCase();
    var pattern=/^[a-zA-Z][a-zA-Z0-9_]{1,14}[a-zA-Z0-9]$/i;
    var patternLastChar = /^[a-zA-Z0-9_]{1,15}_$/i;
    var patternFirstChar = /^[0-9_][a-zA-Z0-9_]{1,14}$/i;
    var message = '';

    if (user_name == "") {
        message = '';
    } else if (user_name.indexOf("xx", 0) != -1) {
        message = '为防止被部分防火墙屏蔽，用户名中不能带有 xx';
    } else if (user_name.length < 3) {
        message = '用户名不能少于3位';
    } else if (user_name.length > 10) {
        message = '用户名不能大于10位';
    } else if (patternFirstChar.test(user_name)) {
        message = '用户名必须以字母开头';
    } else if (patternLastChar.test(user_name)) {
        message = '下划线(_)不能放在末尾';
    } else if (!pattern.test(user_name)) {
        message = '只能由3-10位字母、数字或下划线(_)构成';
    } else {
        message = "√";
    }
    return message;
}
function checkUserName(user_account, username_explain) {
    var message = checkUserNameInternal(user_account);

    if (message == "") {
        username_explain.className = "h12";
    } else if (message != "√"){
        username_explain.style.color = "red";
    } else {
        username_explain.style.color = "green";
    }
    username_explain.innerHTML = message;
}
function userNameFocus(username_explain) {
    username_explain.style.color = "gray";
    username_explain.innerHTML = '用户名由3-10位字母(a-z)、数字(0-9)或下划线(_)构成，并且必须以字母开头';
}

//check nickname
function checkUserNickNameInternal(user_nickname) {
    var nick_name = user_nickname.value.toLowerCase();
    var pattern=/^[a-zA-Z][a-zA-Z0-9_]{1,14}[a-zA-Z0-9]$/i;
    var patternLastChar = /^[a-zA-Z0-9_]{1,15}_$/i;
    var patternFirstChar = /^[0-9_][a-zA-Z0-9_]{1,14}$/i;
    var message = '';

    if (nick_name == "") {
        message = '';
    } else if (nick_name.indexOf("xx", 0) != -1) {
        message = '为防止被部分防火墙屏蔽，用户昵称中不能带有 xx';
    } else if (nick_name.length < 3) {
        message = '用户昵称不能少于3位';
    } else if (nick_name.length > 10) {
        message = '用户昵称不能大于10位';
    } else if (patternFirstChar.test(nick_name)) {
        message = '用户昵称必须以字母开头';
    } else if (patternLastChar.test(nick_name)) {
        message = '下划线(_)不能放在末尾';
    } else if (!pattern.test(nick_name)) {
        message = '只能由3-10位字母、数字或下划线(_)构成';
    } else {
        message = "√";
    }
    return message;
}
function checkUserNickName(user_nickname, nickname_explain) {
    var message = checkUserNickNameInternal(user_nickname);

    if (message == "") {
        nickname_explain.className = "h12";
    } else if (message != "√"){
        nickname_explain.style.color = "red";
    } else {
        nickname_explain.style.color = "green";
    }
    nickname_explain.innerHTML = message;
}
function userNickNameFocus(nickname_explain) {
    nickname_explain.style.color = "gray";
    nickname_explain.innerHTML = '用户昵称由3-10位字母(a-z)、数字(0-9)或下划线(_)构成，并且必须以字母开头';
}

//check password
function checkPasswordInternal(passwordObj) {
    var password = passwordObj.value;
    var numericPattern = /^[0-9]{1,9}$/i;
    var repeatPattern = /^[\w]*(\w)\1{3,}[\w]*$/i;
    var message = '';

    if (password == "") {
        message = '';
    } else if (password.indexOf(" ", 0) != -1) {
        message = '密码不能包含空格';
    } else if (password.length < 6) {
        message = '密码至少要6位以上';
    } else if (numericPattern.test(password)) {
        message = '不能使用纯数字，这样容易被人猜到';
    } else if (repeatPattern.test(password)) {
        message = '太多连续或重复的字符(如：1234、abcd、3333、ssss等)';
    } else {
        message = "√";
    }
    return message;
}
function checkPassword(passwordObj, passwordExplainObj) {
    var message = checkPasswordInternal(passwordObj);

    if (message == "") {
        passwordExplainObj.className = "h12";
    } else if (message != "√"){
        passwordExplainObj.style.color = "red";
    } else {
        passwordExplainObj.style.color = "green";
    }
    passwordExplainObj.innerHTML = message;
}
function userPasswordFocus(passwordExplainObj) {
    passwordExplainObj.style.color = "gray";
    passwordExplainObj.innerHTML =  '密码要至少6位以上，且不能包含空格，英文字母区分大小写';
}

//check password confirm
function checkPasswordConfirmInternal(password1Obj, password2Obj)
{
    var password1 = password1Obj.value;
    var password2 = password2Obj.value;
    var message = '';

    if (password2 == "")
    {
        message = '';
    } else if (password1 != password2) {
        message = '与上次输入的密码不一致';
    } else {
        message = "√";
    }
    return message;
}
function checkPasswordConfirm(password1Obj, password2Obj, password2ExplainObj)
{
    var message = checkPasswordConfirmInternal(password1Obj, password2Obj);

    if (message == "") {
        password2ExplainObj.className = "h12";
    } else if (message != "√"){
        password2ExplainObj.style.color = "red";
    } else {
        password2ExplainObj.color = "green";
    }
    password2ExplainObj.innerHTML = message;
}

//check email
function checkEmailInternal(emailObj) {
    var email = emailObj.value;
    var message = '';

    if (email == "") {
        message = '';
    } else if (!isMail(email)) {
        message = '您的电子邮箱写错了，请修改';
    } else {
        message = "√";
    }
    return message;
}
function checkEmail(emailObj, emailExplainObj) {
    var message = checkEmailInternal(emailObj);

    if (message == "") {
        emailExplainObj.className = "h12";
    } else if (message != "√"){
        emailExplainObj.style.color = "red";
    } else {
        emailExplainObj.style.color = "green";
    }
    emailExplainObj.innerHTML = message;
}
function emailFocus(emailExplainObj) {
    emailExplainObj.style.color = "gray";
    emailExplainObj.innerHTML = "如果您有邮箱，请认真填写，它将用于密码保护";
}

//check register info
function checkInfo(accountObj, nicknameObj, password1Obj, password2Obj, emailObj, codeObj) {
    var message = checkUserNameInternal(accountObj);
    if (message != "√"){
        if (message == "")
        {
            alert("用户名不能为空");
        } else {
            alert(message);
        }
        accountObj.focus();
        return false;
    }
    message = checkUserNickNameInternal(nicknameObj);
    if (message != "√"){
        if (message == "")
        {
            alert("用户昵称不能为空");
        } else {
            alert(message);
        }
        nicknameObj.focus();
        return false;
    }
    message = checkPasswordInternal(password1Obj);
    if (message != "√"){
        if (message == "")
        {
            alert("密码不能为空");
        } else {
            alert(message);
        }
        password1Obj.focus();
        return false;
    }
    message = checkPasswordConfirmInternal(password1Obj, password2Obj);
    if (message != "√"){
        if (message == "")
        {
            alert("密码确认不能为空");
        } else {
            alert(message);
        }
        password2Obj.focus();
        return false;
    }
    message = checkEmailInternal(emailObj);
    if (message != "√"){
        if (message == "")
        {
            alert("邮箱不能为空");
        } else {
            alert(message);
        }
        emailObj.focus();
        return false;
    }

    var code = trim(codeObj.value);
    if (code == ""){
        alert("请输入验证码!");
        codeObj.focus();
        return false;
    }

    return true;
}
